//
//  JKCellStoreHotProduct.m
//  chequanbao_ios
//
//  Created by 朱凯 on 2018/1/29.
//  Copyright © 2018年 com.itboye. All rights reserved.
//

#import "JKCellStoreHotProduct.h"

// vc
#import "JKStoreProductDetailVC.h"

@implementation JKCellStoreHotProduct

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setupUI {
    
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UIControl *last = nil;
    for (int i = 0; i < _products.count; i ++) {
        
        UIControl *view = [self basalViewWithTag:i + 10];
        [self.contentView addSubview:view];
        
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            
            if (last) {
                // 每一行的第一个，last是上一行的右侧那个
                if (i % 4 == 0) {
                    make.left.equalTo(@10);
                    make.width.equalTo(last);
                    make.top.equalTo(last.mas_bottom).offset(5);
                }
                // last是本行的前一个
                else {
                    make.left.equalTo(last.mas_right).offset(5);
                    make.width.equalTo(last);
                    make.top.equalTo(last);
                }
            } else {
                make.left.equalTo(@10);
                make.width.equalTo(@((SCREEN_WIDTH - 10 * 2 - 5 * 3) / 4));
                make.top.equalTo(@10);
            }
            
            // 设置cell高度
            if (i == _products.count - 1) {
                make.bottom.equalTo(@-10);
            }
        }];
        
        last = view;
    }
}
- (UIControl *)basalViewWithTag:(NSInteger)tag {
    
    UIControl *basalView = [UIControl new];
    basalView.tag = tag;
    
    [basalView addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    /*
     "id": "3",
     "name": "吃吃吃",
     "price1": "1300",
     "price2": "1200",
     "detail": "120人已付款",
     "img": "1323",
     "notes": "120人已付款",
     "create_time": "0",
     "update_time": "0",
     "status": "0",
     "discount": "1",
     "recommend": "1",
     "product_type": "2"
     */
    NSDictionary *dic = _products[tag - 10];
    
    // 商家图片
    UIImageView *iconView = [UIImageView new];
    [iconView setImageWithURL:[NSURL URLWithString:BOYE_PICTURE_VIEW_URL(dic[@"img"])] placeholderImage:DEFAULT_IMAGE completed:nil];
    iconView.layer.cornerRadius = 2;
    iconView.layer.masksToBounds = true;
    [basalView addSubview:iconView];
    
    // 名字
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.text = dic[@"name"];
    nameLabel.font = FONT(14);
    nameLabel.textAlignment = NSTextAlignmentLeft;
    nameLabel.textColor = [UIColor blackColor];
    [basalView addSubview:nameLabel];
    
    // 人均消费
    UILabel *avgeLabel = [[UILabel alloc] init];
    avgeLabel.text = dic[@"detail"];
    avgeLabel.font = FONT(12);
    avgeLabel.textAlignment = NSTextAlignmentLeft;
    avgeLabel.textColor = [UIColor lightGrayColor];
    [basalView addSubview:avgeLabel];
    
    // 介绍
    UILabel *descLabel = [[UILabel alloc] init];
    descLabel.text = dic[@"notes"];
    descLabel.numberOfLines = 2;
    descLabel.font = FONT(12);
    descLabel.textAlignment = NSTextAlignmentLeft;
    descLabel.textColor = [UIColor lightGrayColor];
    [basalView addSubview:descLabel];
    
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(@0);
        make.width.equalTo(basalView);
        make.height.equalTo(iconView.mas_width);
    }];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@5);
        make.right.equalTo(@-5);
        make.top.equalTo(iconView.mas_bottom).offset(2);
        make.height.equalTo(@15);
    }];
    [avgeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@0);
        make.left.right.height.equalTo(nameLabel);
        make.top.equalTo(nameLabel.mas_bottom).offset(2);
    }];
    [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(avgeLabel.mas_bottom).offset(2);
        make.left.right.height.equalTo(nameLabel);
        // 设置basalView的高度
        make.bottom.equalTo(@-5);
    }];
    
    return basalView;
}
- (void)tapAction:(UIControl *)view {
    
    NSDictionary *dic = _products[view.tag - 10];
    
    ByBaseVC *sv = (ByBaseVC *)self.viewController;
    JKStoreProductDetailVC *vc = [JKStoreProductDetailVC new];
    [sv by_normalPush:vc hasNavi:true];
}



- (void)setProducts:(NSArray *)products {
    
    if (_products != products) {
        _products = products;
        [self setupUI];
    }
}

@end
